
import {connect} from 'react-redux';
import {increment,decrement,incrementAsync} from '../../redux/actions/count';
import React, { Component } from 'react';

  //定义UI组件
 class Count extends Component {
    //按住alt键，可以选择多个
    state={other:'其他参数'}
    increment=()=>{
       
        const {value}=this.selectNum;
        this.props.increment(value*1)
    }
    decrement=()=>{
        const {value}=this.selectNum;
        this.props.decrement(value*1)
    }
    incrementIfOdd=()=>{
        const {value}=this.selectNum;
        const {count}=this.props
        if(count%2!==0){
            this.props.increment(value*1)
        }
    }
    incrementAsync=()=>{
        const {value}=this.selectNum;  
        this.props.incrementAsync(value*1,500)
    }
    render() {
        const {count}=this.props
        console.log('22',this.props);
        return (
            <div>
                <h2>我是Count组件</h2>
                <h1>当前求和:{count} </h1>
                <select ref={c=>{this.selectNum=c}}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
               <button onClick={this.increment}>+</button>
               <button onClick={this.decrement}>-</button>
               <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>
               <button onClick={this.incrementAsync}>异步加</button>
            </div>
        );
    }
}


    const CountContainer =connect( //react-redux 会自动将对象进行转发到redux的reducers组件中
        state=>({count:state.count}),//用来获取store中的状态，并设置key value键值对
    
            {
                increment,
                decrement,
                incrementAsync
            }  //定义操作方法，每个操作方法作为key，action对象作为value，并自动传到reducer中
        )(Count)

export default CountContainer
